package com.gxc.service.impl;

import com.gxc.entity.User;
import com.gxc.entity.UserDto;
import com.gxc.jpa.UserRepository;
import com.gxc.service.UserService;
import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import javax.annotation.Resource;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

/**
 * UserServiceImpl.
 *
 * @author 宫新程
 * @since 2019/3/21 19:21
 */
@Service
public class UserServiceImpl implements UserService {

  public static void main(String[] args) {
  }

  @Resource private UserRepository userRepository;

  /** 使用User实体接收 做新增或者更新操作. */
  @Override
  public Object updateEntityUser(User user) {
    return userRepository.save(user);
  }

  /** Service 层 new 一个新的Entity. */
  @Override
  public Object updateServiceUser() {

    for (int i = 0; i < 5; i++) {
      User user = new User();
      user.setId(1);
      user.setPassword("zzzzzz");
      user.setLoginDate(new Date());
      user.setUsername("aaaaaa");
      user.setRole(1);
      userRepository.save(user);
    }

    int i = 1 / 0;

    User user2 = new User();
    user2.setPassword("222222");
    user2.setLoginDate(new Date());
    user2.setUsername("222222");
    user2.setRole(2222);
    User user = new User();
    user.setId(1);
    user.setPassword("zzzzzz");
    user.setLoginDate(new Date());
    user.setUsername("aaaaaa");
    user.setRole(1);
    List<User> userList = new ArrayList<>();
    userList.add(user);
    userList.add(user2);
    userRepository.saveAll(userList);
    System.out.println(userList);

    return userList;
  }

  @Override
  public Object updateDaoUser(UserDto userDto) {
    User user = new User();
    user.setId(userDto.getId());
    user.setUsername(userDto.getUsername());
    user.setPassword(userDto.getPassword());
    return userRepository.save(user);
  }
}
